<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include 'secure/access_securities.php';
class Control_employers extends Access_securities {

	public function __construct(){
		parent::__construct("control-employers");
		$this -> form_validation -> set_error_delimiters('<p class="help-block danger">', '</p>');
	}
	public function index()
	{
		$data['title']="Manage Employers";
		$data['employers']=$this->model_globals->select_join_get_only_fields('employers', $arr_fields = array('employer_id','username','email','employers.status','name','logo'),array('companies' => array('employer_id' => 'using_employer_id')), $join_type = 'INNER', $arr_where = NULL, $limit = NULL,$sort='employer_id DESC')->result();
		$this->load->view(B_MASTER."index",$data);
	}
	public function insert_employer(){
		$data['title']="Create Employer";
		$module_id = str_replace('-', '_', $this -> uri -> segment(1));
		$this -> is_has_modify_permission($module_id);
		
		$this->form_validation->set_rules('username', 'Username', 'required');
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[employers.email]');
		$this->form_validation->set_rules('company', 'company', 'required|min_length[2]|max_length[50]');
		$this->form_validation->set_rules('password', 'Password', 'required|matches[conf_password]');
		$this->form_validation->set_rules('conf_password', 'Password Confirmation', 'required');
			
		if ($this -> form_validation -> run() === FALSE) {
			$this->load->view(B_MASTER."index",$data);
		} else {
			$token_key="";
			$data_employer=array(
				'username'=>$this->input->post('username',TRUE),
				'email'=>$this->input->post('email',TRUE),
				'password'=>sha1($this->input->post('password',TRUE)),
				//'subscribe'=>$this->input->post('subscribe',TRUE),
				'is_sub_account'=>'NO',
				'using_sub_account_of_employer'=>0,
				'status'=>1,
				'token_key'=>$token_key,
				'created_date'=>null
			);
			
			$is_inserted_employer_with_return_id=$this->model_globals->insert_get_last_id('employers',$data_employer);
			
			if($is_inserted_employer_with_return_id){
				
				$data_company=array(
					'using_employer_id'=>$is_inserted_employer_with_return_id,
					'name'=>$this->input->post('company',TRUE),
					'description'=>$this->input->post('company_desc',TRUE),
					'address'=>$this->input->post('address',TRUE),
					'phone'=>$this->input->post('phone',TRUE),
					'fax'=>$this->input->post('fax',TRUE),
					'website'=>$this->input->post('website',TRUE),
					'logo'=>'logo.jpg',
					'status'=>1,
					'created_date'=>null
				);
				$is_inserted_employer_contact_company=$this->model_globals->insert('companies',$data_company);
				if($is_inserted_employer_contact_company){
					$this -> session -> set_flashdata('success', 'New employer  was created success !.');
					redirect('control-employers');
				}
				
				$this -> session -> set_flashdata('error', 'New employer contact  was created fail !.');
				redirect('control-employers');
			}else{
				$this -> session -> set_flashdata('error', 'New employer  was created fail !.');
				redirect('control-employers');
			}
		}
	}
	public function update_employer(){
		$data['title']="Update Employer";
		$module_id = str_replace('-', '_', $this -> uri -> segment(1));
		$this -> is_has_modify_permission($module_id);
		
		$employer_id=$this->uri->segment(3);
		
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[employers.email]');
		$this->form_validation->set_rules('company', 'company', 'required|min_length[2]|max_length[50]');
		$this->form_validation->set_rules('password', 'Password', 'required|matches[conf_password]');
		$this->form_validation->set_rules('conf_password', 'Password Confirmation', 'required');
			
		if ($this -> form_validation -> run() === FALSE) {
			$data['employer_info']=$this->model_globals->select_join_get_only_fields('employers', $arr_fields = array('employer_id','username','email','employers.status','name','logo','website','description','address','phone','fax'),array('companies' => array('employer_id' => 'using_employer_id')), $join_type = 'INNER', $arr_where = array('employer_id'=>$employer_id), $limit = NULL,$sort='employer_id DESC')->result();
			$this->load->view(B_MASTER."index",$data);
		} else {
			
			$token_key="";
			$data_employer=array(
				'username'=>$this->input->post('username',TRUE),
				'email'=>$this->input->post('email',TRUE),
				'password'=>sha1($this->input->post('password',TRUE))
				//'subscribe'=>$this->input->post('subscribe',TRUE),
				//'is_sub_account'=>'NO',
				//'using_sub_account_of_employer'=>0,
				//'status'=>1,
				//'token_key'=>$token_key,
				//'created_date'=>null
			);
			
			$is_updated_employer=$this->model_globals->update('employers',$data_employer,array('employer_id'=>$employer_id));
			
			if($is_updated_employer){
				
				$data_company=array(
					//'using_employer_id'=>$is_inserted_employer_with_return_id,
					'name'=>$this->input->post('company',TRUE),
					'description'=>$this->input->post('company_desc',TRUE),
					'address'=>$this->input->post('address',TRUE),
					'phone'=>$this->input->post('phone',TRUE),
					'fax'=>$this->input->post('fax',TRUE),
					'website'=>$this->input->post('website',TRUE),
					'logo'=>'logo.jpg'
					//'status'=>1,
					//'created_date'=>null
				);
				
				$is_updated_employer_contact=$this->model_globals->update('companies',$data_company,array('using_employer_id'=>$employer_id));
				if($is_updated_employer_contact){
					
					$this -> session -> set_flashdata('success', 'Employer information was updated success !.');
					redirect('control-employers');
				}
				
				$this -> session -> set_flashdata('error', 'Employer contact company  was updated fail !.');
				redirect('control-employers');
			}else{
				$this -> session -> set_flashdata('error', 'Employer  information was updated fail !.');
				redirect('control-employers');
			}
			
		}
	}
	public function delete_employer(){
		$employer_id=$this->uri->segment(3);
		$is_deleted=$this->model_globals->delete('employers',array('employer_id'=>$employer_id));
		if($is_deleted){
			$this -> session -> set_flashdata('success', 'Employer information was deleted success !.');
			redirect('control-employers');
		}else{
			$this -> session -> set_flashdata('error', 'Employer information  was deleted fail !.');
			redirect('control-employers');
		}
	}
	public function disabled_employer(){
		$employer_id=$this->uri->segment(3);
		$data=array('status'=>0);
		$is_disabled=$this->model_globals->update('employers',$data,array('employer_id'=>$employer_id));
		if($is_disabled){
			$this -> session -> set_flashdata('success', 'Employeer  was disabled success !.');
			redirect('control-employers');
		}else{
			$this -> session -> set_flashdata('error', 'Employeer  was disabled fail !.');
			redirect('control-employers');
		}
	}
	public function enabled_employer(){
		$employer_id=$this->uri->segment(3);
		$data=array('status'=>1);
		$is_disabled=$this->model_globals->update('employers',$data,array('employer_id'=>$employer_id));
		if($is_disabled){
			$this -> session -> set_flashdata('success', 'Employeer  was enabled success !.');
			redirect('control-employers');
		}else{
			$this -> session -> set_flashdata('error', 'Employeer  was enabled fail !.');
			redirect('control-employers');
		}
	}
	
}
